package main

import (
	"sort"
)

func main() {
	println(bestTeamScore([]int{4, 5, 6, 5}, []int{2, 1, 2, 1}))
}

type player struct {
	score int
	age   int
}

/**
 *	bestTeamScore
 *	@Description: 排序 + 动态规划
 *	@param scores []int
 *	@param ages []int
 *	@return ans int
 */
func bestTeamScore(scores []int, ages []int) (ans int) {
	n := len(scores)
	players := make([]player, n)
	for i := range scores {
		players[i].score = scores[i]
		players[i].age = ages[i]
	}
	sort.Slice(players, func(i, j int) bool {
		return players[i].age < players[j].age || players[i].age == players[j].age && players[i].score < players[j].score
	})
	dp := make([]int, n)
	dp[0] = players[0].score
	for i := 1; i < n; i++ {
		dp[i] = players[i].score
		for j := 0; j < i; j++ {
			if players[i].score >= players[j].score {
				dp[i] = max(dp[i], dp[j]+players[i].score)
			}
		}
	}
	for _, v := range dp {
		ans = max(ans, v)
	}
	return
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
